$("#couponChoose").click(function () {
    let userId = $("input[name='userIdHid']").val();
    $.ajax({
        type: 'post',
        url: '/fr/coupon/' + userId + '',
        success: function (data) {
            let list = eval(data.data)
            let str =""
            $.each(list, function (idx, item) {
                console.log("item->", item)
                str += "<input type='hidden' name='couponIdHid' value='"+item.id+"'>"+
                    "      <input type=\"radio\" name=\"couponRadio\" value='"+item.quota+"' onclick='get_Price("+item.quota+")'>\n" +
                    "                    <div class=\"coupon coupon-yellow2 \">\n" +
                    "                       <div class=\"coupon-info coupon-wave-right \">" +
                    "                          <div class=\"coupon-price\"> ¥" + item.quota + " </div>\n" +
                    "                             <div class=\"coupon-desc\"> " + item.name + "</div>\n" +
                    "                            <div class=\"coupon-expiry-date\">有效时间至" + item.effectiveDays + "</div> \n" +
                    "                       </div>" +
                    "                    <div class=\"coupon-get\"> \n" +
                    "                           <span >使用</span> \n" +
                    "                    </div>\n" +
                    "                    </div>\n";
            })
            $("#youhuiquan").html(str);
        }
    })
})

function get_Price(quota){
    let oldPrice = $("input[name='oldPrice']").val();
    if (quota!=null){
        let realPrice = oldPrice - quota;
        $("#realPrice").text(realPrice);
    }else $("#realPrice").text(oldPrice);

}
$("#payAli").click(function () {
    let info = $("input[name='goodsInfoHid']").val();
    let body = info.substring(0,10)+"...";
    let couponIdHid = $("input[name='couponRadio']:checked").prev().val();
    let ordCode = $("input[name='ordCodeHid']").val();
    // console.log(couponIdHid);
    if (couponIdHid!=null){
        $.ajax({
            type:"post",
            url:"/fr/order/blind/"+ordCode+"/"+couponIdHid+"",
            success:function () {
                window.location.href = "http://localhost:8081/fr/aliPay/"+ $("input[name='ordCodeHid']").val()+"/"+$("#realPrice").text()+"/"+ $("input[name='goodsNameHid']").val()+"/"+ body+"";
            }
        })
    }else {
        window.location.href = "http://localhost:8081/fr/aliPay/"+ $("input[name='ordCodeHid']").val()+"/"+$("#realPrice").text()+"/"+ $("input[name='goodsNameHid']").val()+"/"+ body+"";
    }
})

$(function () {
    $("#order").click(function(){
        let userId = $("input[name='userIdHid']").val();
        let shopId = $("input[name='shopIdHid']").val();
        let goodsId = $("input[name='goodsIdHid']").val();
        let ordPrice=document.getElementById('price').value;
        if (userId.length==0){
            alert("sorry,请先登录！")
        }else if(userId.length>0){
            $.ajax({
                type: 'post',
                url: '/fr/pay/createOrd/'+ordPrice+'/'+userId+'/'+shopId+'/'+goodsId+'',
                success:function (res) {
                    console.log(res.data);
                    alert("下单成功,请在二十分钟内尽快支付！");
                }
            })
        } else {
            alert("sorry")
        }
    })
});

$(function () {
    $("#noworder").click(function(){
        let userId = $("input[name='userIdHid']").val();
        let shopId = $("input[name='shopIdHid']").val();
        let goodsId = $("input[name='goodsIdHid']").val();
        let ordPrice=document.getElementById('price').value;
        if (userId.length==0){
            alert("sorry,请先登录！")
        }else if(userId.length>0){
            $.ajax({
                type: 'post',
                url: '/fr/pay/createOrd/'+ordPrice+'/'+userId+'/'+shopId+'/'+goodsId+'',
                success:function () {
                    window.location.href ="/fr/order";
                }
            })
        } else {
            alert("sorry")
        }
    })
});


